package com.example.leo.mycharmingstudent.a0901;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leo.mycharmingstudent.R;

import java.util.Locale;
import java.util.zip.Inflater;

public class DialogActivity extends AppCompatActivity {

    //一般样式对话框   按钮
    private TextView tvCommonStyle;
    //列表对话框
    private TextView tvList;
    //单选对话框
    private TextView tvRadio;
    //多选对话框
    private TextView tvMultiple;
    //自定义AlertDialog对话框   按钮
    private TextView tvCustomAlert;
    //自定义的AlertDialog对象
    private AlertDialog mCustomAlertDialog;
    //自定义Dialog对话框 按钮
    private TextView tvCustomDialog;
    //popupWindow  按钮
    private TextView tvPopupWindow;
    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        tvCommonStyle = findViewById(R.id.tv_common_style_dialog_activity_dialog);
        tvList = findViewById(R.id.tv_list_style_dialog_activity_dialog);
        tvRadio = findViewById(R.id.tv_radio_style_dialog_activity_dialog);
        tvMultiple = findViewById(R.id.tv_multiple_style_dialog_activity_dialog);
        tvCustomAlert = findViewById(R.id.tv_custom_alert_style_dialog_activity_dialog);
        tvCustomDialog = findViewById(R.id.tv_custom_dialog_style_dialog_activity_dialog);
        tvPopupWindow = findViewById(R.id.tv_popup_window_activity_dialog);
    }

    private void initData() {

    }

    private void initEvent() {
        tvCommonStyle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示一般样式的对话框
                showCommonStyleDialog();
            }
        });
        tvList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示列表样式的对话框
                showListStyleDialog();
            }
        });
        tvRadio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示单选样式的对话框
                showRadioStyleDialog();
            }
        });
        tvMultiple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示多选样式的对话框
                showMultipleStyleDialog();
            }
        });
        tvCustomAlert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示自定义AlertDialog对话框
                showCustomAlertDialog();
            }
        });
        tvCustomDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示自定义Dialog对话框
                CustomDialog customDialog = new CustomDialog(
                        DialogActivity.this,
                        R.style.CustomDialog);
                customDialog.show();
            }
        });
        tvPopupWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示PopupWindow
                showPopupWindow();
            }
        });
    }

    /**
     * 显示PopupWindow
     */
    private void showPopupWindow() {
        View popupWindowView = LayoutInflater
                .from(DialogActivity.this)
                .inflate(R.layout.popup_window_layout, null, false);
        TextView tvFirst = popupWindowView.findViewById(R.id.tv_first_popup_window_layout);
        tvFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(
                        DialogActivity.this,
                        "老子天下第一",
                        Toast.LENGTH_LONG).show();
                //dismiss方法，使popupwindow消失
                mPopupWindow.dismiss();
            }
        });
        TextView tvSecond = popupWindowView.findViewById(R.id.tv_second_popup_window_layout);
        tvSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(
                        DialogActivity.this,
                        "老子天下第二",
                        Toast.LENGTH_LONG).show();
                //dismiss方法，使popupwindow消失
                mPopupWindow.dismiss();
            }
        });
//        View.inflate()//用这个方法也可以创建View
        mPopupWindow = new PopupWindow(
                popupWindowView,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                true);
        /////////////////////////////////////////////////////////
        //如果显示popupwindow以后，点击外部区域或者点击返回按钮不消失，
        //添加一下两句代码
        //设置背景
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置外部可触摸
        mPopupWindow.setOutsideTouchable(true);
        ////////////////////////////////////////////////////////

        //设置能响应View的点击事件
        mPopupWindow.setTouchable(true);
        mPopupWindow.setAnimationStyle(R.style.PopupWindowAnim);
        /*
        1.showAsDropDown(tvPopupWindow,40,-200) （这个得记住）
        40表示横向偏移40个px像素
        -200表示纵向负方向偏移200个px像素
        2.代码当中看到数字单位，一定px（了解）
         */
        mPopupWindow.showAsDropDown(tvPopupWindow, 40, -200);
    }

    /**
     * 显示自定义AlertDialog对话框
     */
    private void showCustomAlertDialog() {
        View customView = LayoutInflater.from(this)
                .inflate(R.layout.custom_alert_dialog_layout, null, false);
        TextView tvContent = customView.findViewById(R.id.tv_content_custom_alert_dialog_layout);
        tvContent.setText("点我退出？");
        tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(
                        DialogActivity.this,
                        "嘿，我真是个按钮",
                        Toast.LENGTH_LONG).show();
                mCustomAlertDialog.cancel();
            }
        });
        //1.创建构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //2.构建  setTitle等方法
//        builder.setTitle("自定义AlertDialog对话框");
        builder.setView(customView);
        builder.setPositiveButton("你点我试试", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(
                        DialogActivity.this,
                        "嘿，你还真敢点",
                        Toast.LENGTH_LONG).show();

            }
        });
        //2.显示  show方法
        mCustomAlertDialog = builder.show();
    }

    /**
     * 显示多选样式的对话框
     */
    private void showMultipleStyleDialog() {
        final String[] food = new String[]{"锅包肉", "宫保鸡丁", "42元的可乐"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("列表样式对话框");
        builder.setMultiChoiceItems(food, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                String format = String
                        .format(Locale.CHINA,
                                        /*Locale.CHINA,*/"%s---%d",
                                //name报错  是因为匿名内部类里面访问局部变量的时候，
                                //这个局部变量必须是final类型
                                food[which],
                                which);
                Toast.makeText(DialogActivity.this, format, Toast.LENGTH_LONG).show();
            }
        });
        builder.show();
    }

    /**
     * 显示单选样式的对话框
     */
    private void showRadioStyleDialog() {
        final String[] name = new String[]{"张三", "李四", "王二麻子"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("单选样式对话框");
//        name[3];//数组越界错误
        builder.setSingleChoiceItems(name, -1, new DialogInterface.OnClickListener() {
            /**
             * which表示数组的角标
             */
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String format = String
                        .format(Locale.CHINA,
                                        /*Locale.CHINA,*/"%s---%d",
                                //name报错  是因为匿名内部类里面访问局部变量的时候，
                                //这个局部变量必须是final类型
                                name[which],
                                which);
                Toast
                        .makeText(
                                DialogActivity.this,
                                format,
                                Toast.LENGTH_LONG)
                        .show();
                //取消对话框
                dialog.dismiss();
            }
        });
        builder.show();

    }

    /**
     * 显示列表样式的对话框
     */
    private void showListStyleDialog() {
        final String[] food = new String[]{"锅包肉", "宫保鸡丁", "42元的可乐"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("列表样式对话框");
        builder.setItems(food, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String format = String
                        .format(Locale.CHINA,
                                        /*Locale.CHINA,*/"%s---%d",
                                //name报错  是因为匿名内部类里面访问局部变量的时候，
                                //这个局部变量必须是final类型
                                food[which],
                                which);
                Toast
                        .makeText(
                                DialogActivity.this,
                                format,
                                Toast.LENGTH_LONG)
                        .show();
                dialog.cancel();
            }
        });
        builder.show();
    }

    /**
     * 显示一般样式的对话框
     */
    private void showCommonStyleDialog() {
        //AlertDialog的使用流程：
        //1.先创建一个构建者  相当于秘书 完成各位老板的合理要求
        AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
        //2.构建  秘书开始着手去做各位老板的合理需求
        //怎么做呢？调用相关的方法  setTitle setIcon setMessage  set...Button
        builder.setTitle("一般样式对话框");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setMessage("这是一般样式的对话框，晓得不？");
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    /**
                     * which 1 表示确定键  2表示取消键  3表示中立键
                     */
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String format = String
                                .format(
                                        /*Locale.CHINA,*/"各位老板觉得行---%d",
                                        which);
                        Toast
                                .makeText(
                                        DialogActivity.this,
                                        format,
                                        Toast.LENGTH_LONG)
                                .show();
                        //dismiss()和cancel()效果一样，用哪个都行
//                                dialog.dismiss();
                        dialog.cancel();
                    }
                });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String format = String
                                .format(
                                        /*Locale.CHINA,*/"各位老板觉得BU行---%d",
                                        which);
                        Toast
                                .makeText(
                                        DialogActivity.this,
                                        format,
                                        Toast.LENGTH_LONG)
                                .show();
                        dialog.cancel();
                    }
                });
        builder.setNeutralButton("中立按钮",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String format = String
                                .format(
                                        /*Locale.CHINA,*/"各位老板觉得  凑活吧---%d",
                                        which);
                        Toast
                                .makeText(
                                        DialogActivity.this,
                                        format,
                                        Toast.LENGTH_LONG)
                                .show();
                        dialog.cancel();
                    }
                });
        //3.显示   秘书工作已完成，向各位老板展示成果
        builder.show();

        //3.显示   秘书工作已完成，向各位老板展示成果
//                AlertDialog alertDialog = builder.create();
//                alertDialog.show();
    }
}
